﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'proc_cm_SaveRotatingBanner')
BEGIN
    PRINT 'Dropping Procedure proc_cm_SaveRotatingBanner'
    DROP  Procedure  proc_cm_SaveRotatingBanner
END
GO

PRINT 'Creating Procedure proc_cm_SaveRotatingBanner'
GO

CREATE PROCEDURE [dbo].[proc_cm_SaveRotatingBanner]
	@pRotatingBannerId uniqueidentifier
	, @pImageFilenameTxt nvarchar(255)
	, @pNavigationUrl nvarchar(255) = null
AS
BEGIN
	SET NOCOUNT ON

	UPDATE
		[dbo].[tbl_rotating_banner]
	SET 
		[image_filename_txt] = @pImageFilenameTxt
		,[navigation_url] = @pNavigationUrl
	WHERE
		[rotating_banner_id] = @pRotatingBannerId
	
	IF @@ROWCOUNT = 0
	BEGIN
		UPDATE
			[dbo].[tbl_rotating_banner]
		SET
			[priority_num] = [priority_num] + 1

		INSERT INTO [dbo].[tbl_rotating_banner]
		(
			[rotating_banner_id]
			,[image_filename_txt]
			,[navigation_url]
			,[priority_num]
		)
		VALUES
		(
			@pRotatingBannerId
			,@pImageFilenameTxt
			,@pNavigationUrl
			,1
		)
	END
END
GO

GRANT EXEC ON dbo.proc_cm_SaveRotatingBanner TO PUBLIC
GO



